package csel.model;

public class Map {
	Tile[][] tileMatrix;
	
	public Map(int width, int height)
	{
		tileMatrix = new Tile[width][height];
	}
	
	public Map(Tile[][] tile)
	{
		tileMatrix = tile;
	}
	
	//return tiles
	public Tile getTile(int x, int y)
	{
		try
		{
			return tileMatrix[x][y];
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			return null;
		}
	}
	
	public Tile[][] getAdjacentTiles(Tile tile, int dx, int dy)
	{
		int dxLength = 2*Math.abs(dx);
		int dyLength = 2*Math.abs(dy);
		Location locTemp = tile.getLocation();
		if(locTemp == null)
		{
			return null;
		}
		
		int xTile = locTemp.getX();
		int yTile = locTemp.getY();
		Tile[][] newTile = new Tile[dxLength+1][dyLength+1];
		
		//logic for getting the coordinates translated. if below dx or dy then it
		//is subtracted a right amount, if above dx or dy then it is added the right amount
		for(int i = 0; i < dxLength; i++)
		{
			for(int j = 0; j <= dyLength; j++)
			{
				int xTemp = xTile - (dx - i);
				int yTemp = yTile - (dy - j);
				try
				{
					newTile[i][j] = tileMatrix[xTemp][yTemp];
				}
				catch(ArrayIndexOutOfBoundsException e)
				{
				}
			}
		}
		return newTile;
	}
	
	public Tile[][] getTileMatrix() {
		return tileMatrix;
	}

	public void setTileMatrix(Tile[][] tileMatrix) {
		this.tileMatrix = tileMatrix;
	}
}
